package com.ym.b2b.buyer.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.buyer.service.EmCBuyerService;
import com.ym.b2b.buyer.sqlbean.EmCBuyer;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

/***
 * @ClassName: ManagerEmCBuyerContorller
 * @Description: 采购商 后台处理
 * @Author Li Junhui
 * @Date 2015年1月5日 下午4:05:13
 * @Modify
 * @CopyRight 珍诚医药在线股份有限公司
 */
@Controller
@RequestMapping("/buyer")
public class ManagerEmCBuyerContorller extends BaseController {

    @Autowired
    private EmCBuyerService emCBuyerService;

    @RequestMapping("showEmCBuyerDetail.do")
    @ResponseBody
    public Result showEmCBuyerDetail(EmCBuyer emCBuyer, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 查看采购商详情："+ emCBuyer.getBuyerCode(), "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.showEmCBuyerDetail(emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /***
     * @Title: searchPageBuyer
     * @Description: 前台 采购商 列表界面
     * @Param @param emCBuyer
     * @Param @param page
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping(value = "searchPageBuyer.do")
    public ResponseEntity<Result> searchPageBuyer(EmCBuyer emCBuyer, Page<EmCBuyer> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCBuyerService.searchPageEmCBuyer(page, emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return new ResponseEntity<Result>(result, getHeader(), HttpStatus.OK);
    }

    @RequestMapping(value = "searchPageBuyerExcel.do")
    @ResponseBody
    public Result searchPageBuyerExcel(EmCBuyer emCBuyer, Page<EmCBuyer> page, String loadType, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 导出采购商列表信息", "USER", 2);
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emCBuyerService.searchPageEmCBuyer(page, emCBuyer);
            ExcelUtil<EmCBuyer> excelUtil = new ExcelUtil<EmCBuyer>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("buyerListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping(value = "getBuyerList.do")
    @ResponseBody
    public Result getBuyerList(EmCBuyer emCBuyer, Page<EmCBuyer> page, HttpServletResponse res) {
        Result result = new Result();
        try {
            result = emCBuyerService.searchPageEmCBuyer(page, emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("insertBuyer.do")
    @ResponseBody
    public Result insertBuyer(EmCBuyer emCBuyer, HttpServletRequest req) {
      	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 新增采购商信息", "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.insertBuyer(emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateBuyer.do")
    @ResponseBody
    public Result updateBuyer(EmCBuyer emCBuyer, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 修改采购商信息", "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.updateBuyer(emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("deleteBuyer.do")
    @ResponseBody
    public Result deleteBuyer(EmCBuyer emCBuyer, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 删除采购商信息", "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.deleteEmCBuyer(emCBuyer);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 批量审批
    @RequestMapping("editMultipleEmCBuyersStatus.do")
    @ResponseBody
    public Result editMultipleEmCBuyersStatus(String buyerIdList, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 批量审批采购商信息", "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.editMultipleEmCBuyersStatus(buyerIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmCBuyers.do")
    @ResponseBody
    public Result deleteMultipleEmCBuyers(String buyerIdList, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 批量删除采购商信息", "USER", 2);
        Result result = new Result();
        try {
            result = emCBuyerService.deleteMultipleEmCBuyers(buyerIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
